using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CSModeling
{
    public partial class Form1 : Form
    {
        private Graphics g;
        private BufferedGraphicsContext currentContext;
        private BufferedGraphics myBuffer;
        private Graphics dc;
        private SMFModel SMF;
        //private Boolean MouseDown;
        public Form1()
        {
            InitializeComponent();
            g = pictureBox1.CreateGraphics();
            currentContext = BufferedGraphicsManager.Current;
            myBuffer = currentContext.Allocate(g, pictureBox1.DisplayRectangle);
            dc = myBuffer.Graphics;
            dc.Clear(Color.White);

        }

        private void button1_Click(object sender, EventArgs e)
        {

            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName != null)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName);
                //StreamReader sr = new StreamReader(@"F:\\Data\\cube.smf");
                if (checkBox1.Checked)
                    SMF = new SMFModel(sr.ReadToEnd(), pictureBox2.BackColor, Coloring.PartiColor);
                else
                    SMF = new SMFModel(sr.ReadToEnd(), pictureBox2.BackColor, Coloring.PureColor);
                if (radioButton1.Checked)
                {
                    SMF.SetIllumination(Illumination.None, dc, pictureBox1.Height, pictureBox1.Width);
                }
                else if (radioButton2.Checked)
                {
                    SMF.SetIllumination(Illumination.PhoneModeling, dc, pictureBox1.Height, pictureBox1.Width);
                }
                else if (radioButton3.Checked)
                {
                    SMF.SetIllumination(Illumination.PhoneModeling, dc, pictureBox1.Height, pictureBox1.Width);
                    SMF.SetShading(Shading.Phong, dc, pictureBox1.Height, pictureBox1.Width);
                }
                else if (radioButton4.Checked)
                {
                    SMF.SetIllumination(Illumination.SimpleFilling, dc, pictureBox1.Height, pictureBox1.Width);
                }
                SMF.Zoom(trackBar1.Value, dc, pictureBox1.Height, pictureBox1.Width);
                SMF.Shift(trackBar2.Value, trackBar3.Value, dc, pictureBox1.Height, pictureBox1.Width);
                SMF.Rotate(trackBar4.Value, trackBar5.Value, trackBar6.Value, dc, pictureBox1.Height, pictureBox1.Width);
                //this.radioButton1.Select();
                //SMF.Refresh(dc, pictureBox1.Height, pictureBox1.Width);
                if (myBuffer != null)
                    myBuffer.Render(g);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (SMF != null)
                SMF.Zoom(trackBar1.Value, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }
        
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            if (SMF != null)
                SMF.Shift(trackBar2.Value, trackBar3.Value, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            if (SMF != null)
                SMF.Shift(trackBar2.Value, trackBar3.Value, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                if (SMF != null)
                    SMF.SetIllumination(Illumination.None, dc, pictureBox1.Height, pictureBox1.Width);
                if (myBuffer != null)
                    myBuffer.Render(g);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                
                if (SMF != null)
                    SMF.SetIllumination(Illumination.PhoneModeling, dc, pictureBox1.Height, pictureBox1.Width);
                if (myBuffer != null)
                    myBuffer.Render(g);
            }

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                if (SMF != null)
                {
                    SMF.SetIllumination(Illumination.PhoneModeling, dc, pictureBox1.Height, pictureBox1.Width);
                    SMF.SetShading(Shading.Phong, dc, pictureBox1.Height, pictureBox1.Width);
                }
                if (myBuffer != null)
                    myBuffer.Render(g);
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                checkBox1.Visible = true;
                if (SMF != null)
                {
                    if (checkBox1.Checked)
                        
                    SMF.SetIllumination(Illumination.SimpleFilling, dc, pictureBox1.Height, pictureBox1.Width);
                }

                if (myBuffer != null)
                    myBuffer.Render(g);
            }
            else
            {
                checkBox1.Visible = false;
            }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (SMF != null)
            {
                if (checkBox1.Checked)
                {
                    SMF.SetColor(Coloring.PartiColor, pictureBox2.BackColor, dc, pictureBox1.Height, pictureBox1.Width);
                }
                else
                {
                    SMF.SetColor(Coloring.PureColor, pictureBox2.BackColor, dc, pictureBox1.Height, pictureBox1.Width);
                }
            }
            if (myBuffer != null)
                myBuffer.Render(g);
        }
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            pictureBox2.BackColor = colorDialog1.Color;
            if (SMF != null)
                SMF.SetColor(colorDialog1.Color, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            if (SMF != null)
                SMF.Rotate(trackBar4.Value, trackBar5.Value, trackBar6.Value, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            if (SMF != null)
                SMF.Rotate(trackBar4.Value, trackBar5.Value, trackBar6.Value, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
            if (SMF != null)
                SMF.Rotate(trackBar4.Value, trackBar5.Value, trackBar6.Value, dc, pictureBox1.Height, pictureBox1.Width);
            if (myBuffer != null)
                myBuffer.Render(g);
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (SMF != null)
            {
                Form_PhongParam form = new Form_PhongParam(SMF.para);
                form.Show();
                SMF.Refresh(dc, pictureBox1.Height, pictureBox1.Width);
                if (myBuffer != null)
                    myBuffer.Render(g);
            }

        }




    }
}